package ExamSystemClient.managerSystem.ExamManagement;

import ExamSystemClient.client.ClientScanner;
import ExamSystemClient.model.ChoiceQuestion;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * 查找信息
 *
 * @author huangshuxian
 */
public class SearchQuestion extends QuestionManage {

    public SearchQuestion(List<ChoiceQuestion> questions) {
        super(questions);
    }

    @Override
    public void operate() {
        //1、提示用户输入需要查询的考题
        System.out.println("请输入查询方式编号（1考题编号/2考题关键字）：");
        int type = ClientScanner.getScanner().nextInt();
        switch(type){
            case 1:
                System.out.println("请输入需要查询的考题编号：");
                int number = ClientScanner.getScanner().nextInt();
                int index = getIndexByNumber(number);
                if (index >= 0){
                    System.out.println("查询到的考题为：");
                    System.out.println(questions.get(index));
                }else{
                    System.out.println("输入的考题编号找不到匹配的数据！");
                }
                break;
            case 2:
                System.out.println("请输入需要查询的考题关键字：");
                String str= ClientScanner.getScanner().next();
                List<ChoiceQuestion> result = new ArrayList<>();
                for (ChoiceQuestion question : questions) {
                    if (question.getStem().contains(str)){
                        result.add(question);
                    }
                }
                if (result.size() == 0){
                    System.out.println("未查到相关考题！");
                    break;
                }
                System.out.println("查询到的考题有：");
                for (ChoiceQuestion question : result) {
                    System.out.println(question);
                }
                break;
                default:
        }

    }

    @Override
    public SearchQuestion show() {
        System.out.println("查询考题信息！");
        return this;
    }
}
